#
# PVCSConfigFileReader.py
#
# this class reads the content of the PVCS config files
# Remarks: This class needs a PVCSConfigFile instance to work
#
# Author: Uwe Stieber
# Copyright (c) 1998, 1999 TakeFive Software, Salzburg
#

# import some system modules
import string
import os
from stat import *

# import PVCS constants and helper functions
import PVCSConstants

class PVCSConfigFileReader:
	# some class privates
	__fileObject = None		 # store the file object of the
								# config file
	__theConfigFileName = "" # name of the config file
	__theDataStore = None    # config file content
	__mtime = 0				 # last modification time of config file

	def __init__(self, aFileName, aDataStore):
		# constructor
		if PVCSConstants.TEST:
			print "(PVCS Config File Reader)@B@g>>>@x __init__@B"
			print "(PVCS Config File Reader)\tconfig file name: @B@b%s@x@B" % aFileName
		self.__theConfigFileName = aFileName
		self.__theDataStore = aDataStore
		try:
			if self.__theConfigFileName != "":
				self.__mtime = os.stat(self.__theConfigFileName)[ST_MTIME]
			else:
				self.__mtime = 0
		except:
			self.__mtime = 0
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x __init__@B"
		return

	def getConfigFileName(self):
		return self.__theConfigFileName

	def analyseConfigFile(self):
		# read every line of the config file
		# and call the analyse method
		if PVCSConstants.TEST:
			print "(PVCS Config File Reader)@B@g>>>@x analyseConfigFile@B"
			print "(PVCS Config File Reader)\tanalyse file: '@B@b%s@x@B'" % self.__theConfigFileName
		try:
			self.__fileObject = open(self.__theConfigFileName)

			# read the lines of the config file
			aLine = self.__fileObject.readline()
			while aLine != "":
				if self.isIncludeLine(aLine):
					# it's a include statement
					# analyse the include config file
					aFileName = self.getIncludedFile(aLine)
					if aFileName != None:
						try:
							aIncludedPVCSConfigFile = PVCSConfigFileReader(aFileName, self.__theDataStore)
							aIncludedPVCSConfigFile.analyseConfigFile()
						except:
							pass
				else:
					# ok, is a 'normal' Line
					self.analyseLine(aLine)
				aLine = self.__fileObject.readline()
			# close the line finally
			self.__fileObject.close()
		except IOError:
			if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x analyseConfigFile (IOError)@B"
			raise IOError
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x analyseConfigFile@B"
		return

	def analyseLine(self, aLine):
		# analyse the given line and
		# store the values inside the datastore
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@g>>>@x analyseLine (aLine = '%s')@B" % aLine
		# split the line
		splittedList = PVCSConstants.split(aLine, ' =\t\n\r\v\f')
		if len(splittedList) > 0:
			# the line contains something, we can try to find
			# out what
			if self.__theDataStore.isValidKey(splittedList[0]):
				# yeah, that's fine. We found a valid key
				if PVCSConstants.TEST: print "(PVCS Config File Reader)\tvalid key found: '@B@b%s@x@B' " % splittedList[0]
				# give splitted line to further analysing to the datastore
				try:
					self.__theDataStore.store(splittedList)
				except PVCSConfigFileSyntaxError:
					print "(PVCS Config File Reader)\tSyntaxError in Line: '@B@b%s@x@B' in File '@B@b%s@x@B'" % (aLine, self.__theConfigFileName)
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x analyseLine@B"
		return

	def isIncludeLine(self, aLine):
		# check, if we have to inculde another file
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@g>>>@x isIncludeLine (aLine = '%s')@B" % aLine
		if string.find(string.lower(aLine), string.lower("Include")) != -1:
			retval = 1
		else:
			retval = 0
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x isIncludeLine (retval = %d)@B" % retval
		return retval

	def getIncludedFile(self, aLine):
		# returns the include file
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@g>>>@x getIncludedFile@B"
		aFile = None
		splittedLine = string.split(aLine)
		if len(splittedLine) >= 2:
			# we need at least 2 items in the list
			aFile = splittedLine[1]
		if aFile != None:
			if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x getIncludedFile (File = %s)@B" % aFile
		else:
			if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x getIncludedFile (File = None)@B"
		return aFile

	def reload(self):
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@g>>>@x reload@B"
		try:
			if self.__theConfigFileName != "":
				# check modification time
				if self.__mtime < os.stat(self.__theConfigFileName)[ST_MTIME]:
					# must reload configuration file
					self.__mtime = os.stat(self.__theConfigFileName)[ST_MTIME]
					self.forceReload()
		except:
			self.__mtime = 0
			self.forceReload()
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x reload@B"
		return

	def forceReload(self):
		# do reload of config file
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@g>>>@x forceReload@B"
		self.__theDataStore.clearSuffixRules()
		self.analyseConfigFile()
		if PVCSConstants.TEST: print "(PVCS Config File Reader)@B@r<<<@x forceReload@B"
		return

def doAction():
	import os
	import PVCSConfigFile
	aTestConfigFileName = os.path.join(os.environ["SNIFF_DIR"], "lib", "python", "Sniff", "Pvcs", "Pvcs4Sniff.cfg")
	aTestDataStore = PVCSConfigFile.PVCSConfigFile()
	aTestConfigFile = PVCSConfigFileReader(aTestConfigFileName, aTestDataStore)
	aTestConfigFile.analyseConfigFile()
	print "ConfigFileName: %s" % aTestConfigFile.getConfigFileName()
	aTestDataStore.printArchiveSuffixList()
	# try to find some suffix matches
	aTestSuffixList = ['.for', '.bat', '.shared', '.java', '.c',
						'.h', '.hpp', '.doc', '.bak', '.dot',
						'.hh', '.', '.asm', '.ddl', '.xxx', '.b',
						'.ca', '.BAK', '.baa']
	for aTestSuffix in aTestSuffixList:
		if aTestDataStore.findSuffixKey(aTestSuffix) != None:
			#print "use '%s'\tfor '%s'" 	% (aTestDataStore.findSuffixKey(aTestSuffix), aTestSuffix)
			print "'%s'\t--> '%s'" % (aTestSuffix, aTestDataStore.calculateArchiveExtension(aTestSuffix))
			#print ""
		else:
			print "No archive suffix rule for '%s' available !" % aTestSuffix

if __name__ == "__main__":
	# run as script
	doAction()
